package com.ostrich.note.ui.settings.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ostrich.note.R;
import com.ostrich.note.databinding.ItemNoteTypeBinding;
import com.ostrich.note.databinding.ItemSettingsTypeBinding;
import com.ostrich.note.ui.note.adapter.NoteTypeEditAdapter;
import com.ostrich.note.ui.note.entity.TypeEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;


public class SettingsTypeAdapter extends RecyclerView.Adapter<SettingsTypeAdapter.SettingsTypeViewHolder> {
    private List<TypeEntity.ListBean> mList = new ArrayList<>();
    private NoteTypeEditAdapter.ItemChildClickListener mItemChildClickListener;
    //添加数据
    public void insertData(TypeEntity.ListBean listBean){
        mList.add(listBean);
        notifyDataSetChanged();
    }
    public void setItemChildClickListener(NoteTypeEditAdapter.ItemChildClickListener itemChildClickListener) {
        mItemChildClickListener = itemChildClickListener;
    }
    //数据做刷新
    public void setData(List<TypeEntity.ListBean> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public TypeEntity.ListBean getDataByPosition(int position){
        return mList.get(position);
    }
    //添加数据
    public void updateData(int position,String title){
        TypeEntity.ListBean listBean = mList.get(position);
        listBean.setTitle(title);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SettingsTypeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSettingsTypeBinding itemNoteTypeBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_settings_type, parent, false);
        return new SettingsTypeViewHolder(itemNoteTypeBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull SettingsTypeViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.mItemSettingsTypeBinding.setNoteType(mList.get(position));

        holder.mItemSettingsTypeBinding.ivMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemChildClickListener != null){
                    mItemChildClickListener.onItemChildClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void deleteData(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    class SettingsTypeViewHolder extends RecyclerView.ViewHolder{
        private ItemSettingsTypeBinding mItemSettingsTypeBinding;
        public SettingsTypeViewHolder(ItemSettingsTypeBinding mItemSettingsTypeBinding) {
            super(mItemSettingsTypeBinding.getRoot());
            this.mItemSettingsTypeBinding = mItemSettingsTypeBinding;


        }
    }

    public interface ItemChildClickListener{
        void onItemChildClick(View view,int position);
    }


}
